<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>

<style>

.select2-drop {
        z-index: 10050 !important;
    }
 
    .select2-search-choice-close {
        margin-top: 0 !important;
        right: 2px !important;
        min-height: 10px;
    }
 
        .select2-search-choice-close:before {
            color: black !important;
        }
    /*防止select2不会自动失去焦点*/
    .select2-container {
        z-index: 16000 !important;
    }
 
    .select2-drop-mask {
        z-index: 15990 !important;
    }
 
    .select2-drop-active {
        z-index: 15995 !important;
    }


.custom-switch {
    padding-left: 3.25rem;
}
.custom-control {
    position: relative;
    display: block;
    min-height: 1.5rem;
    padding-left: 2.5rem;
}
.custom-control-input {
    position: absolute;
    z-index: -1;
    opacity: 0;
}
.custom-control-label {
    position: relative;
    margin-bottom: 0;
    vertical-align: top;
}
.custom-switch .custom-control-label::before {
    left: -3.25rem;
    width: 1.75rem;
    pointer-events: all;
    border-radius: .5rem;
}
.custom-control-label::before {
    position: absolute;
    top: .25rem;
    left: -2.5rem;
    display: block;
    width: 1rem;
    height: 1rem;
    pointer-events: none;
    content: "";
    background-color: #dee2e6;
    border: #adb5bd solid 1px;
    box-shadow: inset 0 0.25rem 0.25rem rgba(0,0,0,.1);
}
.custom-control-label::before, .custom-file-label, .custom-select {
    transition: background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.custom-switch .custom-control-label::after {
    top: calc(.25rem + 2px);
    left: calc(-3.25rem + 2px);
    width: calc(1rem - 4px);
    height: calc(1rem - 4px);
    background-color: #adb5bd;
    border-radius: .5rem;
    transition: background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out,-webkit-transform .15s ease-in-out;
    transition: transform .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
    transition: transform .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out,-webkit-transform .15s ease-in-out;
}
.custom-control-input:checked~.custom-control-label::before {
    color: #fff;
    border-color: #007bff;
    background: #007bff linear-gradient(180deg,#268fff,#007bff) repeat-x;
    box-shadow: none;
}
.custom-switch .custom-control-input:checked~.custom-control-label::after {
    background-color: #dee2e6;
    -webkit-transform: translateX(.75rem);
    transform: translateX(.75rem);
}
.custom-control-label::after {
    position: absolute;
    top: .25rem;
    left: -2.5rem;
    display: block;
    width: 1rem;
    height: 1rem;
    content: "";
    background: no-repeat 50%/50% 50%;
}
</style>

                    <div class="row">
                        <div class="col-sm-12">
                             <div class="box">
                                <div class="box-header with-border">
                                    <div class=" col-sm-6">
                                        <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get', 'id' => 'search_goods')); ?>                                    
                                        
                                        <div class="form-group" style="margin-right:15px;">
                                       
                                        <div class="input-group input-group-sm">
                                                <?php echo form_input($keyword);?>
                                                <span class="input-group-btn">
                                                <select class="form-control input-sm" name="adminid">
                                                    <option value="0">所有用户</option>
                                                    <?php
                                                    foreach($adminlist as $item)
                                                    {
                                                        if($adminid==$item["adminid"])
                                                            $s=" selected";
                                                        else
                                                            $s="";
                                                        
                                                        echo '<option value="'.$item["adminid"].'" '.$s.'>'.getadminshow($item["adminid"]).'</option>';
                                                    }
                                                    ?>
                                                </select>
                                                </span>
                                        </div>                                        
                                        </div>
                                        <div class="form-group" >
                                            <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索分类")); ?>    
                                        </div>
                                        
                                        
                                        <?php echo form_close();?>
                                    </div>
                                    <h3 class="box-title pull-right"><?php echo anchor('backend/products/pagelist/0', '<i class="fa fa-thumbs-up"></i> 已售产品', array('class' => 'btn btn-block btn-sm btn-info btn-flat')); ?></h3>
                                </div>
                                <div class="box-body">
                                       
                                    <table class="table table-striped table-hover" style="margin-top:20px;">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>分类名称</th>
                                                <th>添加时间</th>
                                                <th>最新采集</th>
                                                <th>产品数量</th>
                                                <th>置顶</th>
                                                <th>备注</th>
                                                <th>采集秘钥</th>
                                                <th>所属会员</th>
                                                <?php if ($isadmin==1):?>
                                                <th>锁定</th>
                                                <?php endif;?>

                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php foreach ($data as $item):?>
<tr shopid="<?php echo $item["id"]; ?>">
    <td><?php echo $item["id"]; ?></td>
    <td class="shopname" ondblclick="edit(this,'name')"><?php echo $item["name"]; ?></td>
    <td><?php echo date("m-d H:i",strtotime($item["addtime"])); ?></td>
    <td><?php echo $item["glast"]=="no data"?"没有数据":date("m-d H:i",strtotime($item["glast"])); ?></td>
    <td><?php echo $item["pcount"].'/'.$item["gcount"]; ?></td>

    <td>
    <div class="custom-control custom-switch" style="margin-left:10px;" cid="<?php echo $item["id"];?>">
        <input type="checkbox" class="custom-control-input manbox" id="mtoogle<?php echo $item["id"];?>" <?php if($item["sort"]==1) echo " checked";?>>
        <label class="custom-control-label" for="mtoogle<?php echo $item["id"];?>"></label>
    </div>

    </td>

    <td ondblclick="edit(this,'note')" ><?php echo $item["note"]; ?></td>
    <td>
        <input type="text" readonly="true" value="<?php echo $item["key"]; ?>" class="form-control input-sm" id="key<?php echo $item["id"]; ?>" style="display:inline;width:180px;font-size:9px;"><button data-clipboard-target="#key<?php echo $item["id"]; ?>" class="btn btn-sm btn-warning btn-flat copybtn">复制</button></td>
    <td><?php echo getadminshow($item["adminid"]); ?></td>
    <?php if ($isadmin==1):
        if ($item["islock"]==1)
        {
            $class="lock";
            $style="color:#dd4b39;font-size:1.6em;";
        }
        else
        {
            $class="unlock";
            $style="color:#00a65a;font-size:1.6em;";            
        }
    ?>
    <td class="lockcol"><i class="fa fa-<?php echo $class;?>" style="<?php echo $style;?>" v="<?php echo $item["islock"];?>"></i> </td>
    <?php endif;?>

    <td>
    <?php echo anchor('backend/products/pagelist/'.$item["id"],'产品管理','class="btn btn-xs btn-primary"'); ?>&nbsp&nbsp
    <?php if($item["islock"]==0):?>
    <?php // echo anchor('admin/tbshop/delete/'.$item["id"], "删除",array('onclick' => "return confirm('确定删除该条记录?')","class"=>"btn btn-xs btn-danger"));?>

    <button class="btn-xs btn-flat btn-danger btn-delete" itemid="<?php echo $item["id"];?>"><i class="fa fa-trash-o fa-lg"></i></button>

    <?php endif;?>
    </td>
</tr>
<?php endforeach;?>
                                        </tbody>
                                    </table>
                                    <?php 
                                    if(empty($data)){
                                        echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                                    }
                                    ?>
                                    <?php echo $pager;?>
                                </div>
                            </div>
                         </div>
                    </div>
        


        <?php echo form_open(current_url(), array('class' => 'form-horizontal','onsubmit'=>'return checkform();', 'id' => 'form-create_notice')); ?>
        <div class="row">
             <div class="col-sm-12">
                <div class="box">
       
                 <div class="box-header with-border">
                 <h3 class="box-title">添加产品库分类</h3>
                 </div>

                 <div class="box-body">

                    <div class="row">
                        <div class="col-xs-3">
                        <input type="text" class="form-control" name="name" id="name" placeholder="分类名称">
                        </div>
                        <div class="col-xs-6">
                        <input type="text" class="form-control" name="note" id="note" placeholder="备注">
                        </div>
          
                        <div class="col-xs-3">
                            <input type="hidden" name="action" value="add">
                            <input type="submit" class="pull-right btn btn-flat btn-primary btn-sm" value="添加分类">
                        </div>
                    </div> 

                 </div>
          
              
                </div>
              </div>
            <!-- /.box-body -->
          </div>
        <?php echo form_close();?>
<script>
    function edit(obj,attrName){

        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class","form-control input-sm");
        newobj.value = oldHtml;
        var cid = $(obj).parent().attr("shopid");     
        obj.innerHTML = '';  
        obj.appendChild(newobj);
        newobj.focus(); 

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function(){
            if(this.value==null || this.value==""){
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value?this.value:oldHtml;       
            if(oldHtml != this.value){
                $.ajax({
                    type:"post",
                    url:"/backend/products/categoryajaxedit",
                    dataType:"json",
                    data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                    error:function(){
                    },
                    success:function(){
                    },
                });
            }  

        })
    }


    var checkform = function(){
        checkfild={
        "name":"分类名称",
        "note":"备注",
        };
        for (var index in checkfild) {
            if($("input[name='"+index+"']").val().trim().length==0)
            {
                layer.msg(checkfild[index]+"不能为空");
                return false;
            }
        }
    };

    $("button.btn-delete").on("click",function(){
        var cid=$(this).attr("itemid");
        var row=$(this).parent().parent();
        layer.confirm("确认要删除吗，删除将无法回复？", { title: "确认" }, function (index) {           
            $.ajax({
                type:"post",
                url:"/backend/products/categoryajaxdel",
                dataType:"json",
                data:{"cid":cid},
                error:function(){
                },
                success:function(result){
                    if(result["status"]==1)
                    {
                        row.remove();
                    }
                    else
                    {
                        layer.msg(result["msg"]);
                    }
                },
            });
            layer.close(index); 
        });
    });


    $(function(){
        require(['jquery','clipboard.min'], function($,clipboardlib){
            var clipboard = new clipboardlib('.copybtn');
            clipboard.on('success', function(e) {
                layer.msg("复制成功");
            });
            clipboard.on('error', function(e) {
                layer.msg("复制失败");
            });
        });
    });
</script>

<?php if ($isadmin==1):?>
<script>

    $("input.manbox").on("click",function(){
        var cid = $(this).parent().attr("cid");     
        if($(this).is(":checked"))
        {
            newval=1;
        }
        else
        {
            newval=0;
        }
        $.ajax({
            type:"post",
            url:"/backend/products/ajaxcatedit",
            dataType:"json",
            data:{"attrName":"sort","newHtml":newval,"cid":cid},
            error:function(){
            },
            success:function(){
            },
        });
    });


    $("body").on("click","td.lockcol",function(){
        var shopid=$(this).parent().attr("shopid");
        var shopname=$(this).parent().find("td.shopname").text();
        layer.confirm("确认要锁定分类 "+shopname+" 吗？", { title: "确认" }, function (index) {  
            url="/backend/products/togglelock/"+shopid;
            $.get(url, {},
            function(data){
                layer.close(index); 
                window.location.href = window.location.href;
            }); 
        });
        console.log(shopid);
    });
</script>
<?php endif;?>
